import os
import sys
import os.path

myFolder = os.path.split(os.path.realpath(__file__))[0]
sys.path = [os.path.join(myFolder, 'widgets'),
            os.path.join(myFolder, 'networks'),
            os.path.join(myFolder, 'features'),
            os.path.join(myFolder, 'apis'),
            os.path.join(myFolder, 'logger'),
            os.path.join(myFolder, 'dbManager')
            ] + sys.path
os.chdir(myFolder)

import asyncio
import logging
from quamash import QEventLoop
from base import (QApplication, cacheFolder, QDialog, QFrame, QHBoxLayout, HBoxLayout, QIcon, QLabel, QListWidget,
                  QListWidgetItem,
                  QPushButton, PicLabel, QScrollArea, ScrollArea, Qt, QTabWidget, TableWidget, QVBoxLayout, VBoxLayout,
                  QWidget)
from player import PlayWidgets
from native import NativeMusic
from downloadFrame import DownloadFrame
from addition import SearchLineEdit
from systemTray import SystemTray
from loginFrames import LoginBox
from singsFrameBase import DetailSings
from netEaseSingsFrames import NetEaseSingsArea, NetEaseSearchResultFrame
from xiamiSingsFrames import XiamiSingsArea, XiamiSearchResultFrame
from qqSingsFrames import QQSingsArea, QQSearchResultFrame
from recommendFrames import RecommendFrame
from configMainFeatures import (ConfigWindow, ConfigHeader, ConfigNavigation, ConfigMainContent, ConfigSearchArea,
                                ConfigSystemTray, ConfigDetailSings)
from configNativeFeatures import ConfigNative
from configDownloadFrameFeatures import ConfigDownloadFrame
from configNeteaseFeatures import ConfigNetEase
from configXiamiFeatures import ConfigXiami
from configQQFeatures import ConfigQQ
from configRecommendFrameFeatures import ConfigRecommendFrame
import logger

logger.loggerConfig('logger/running_log.log')
logger = logging.getLogger(__name__)
logger.info("当前图片缓存目录: {0}".format(os.path.join(os.getcwd(), cacheFolder)))


class Window(QWidget):

    def __init__(self):
        super(Window, self).__init__()
        self.setObjectName('MainWindow')
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setWindowIcon(QIcon('resource/format.ico'))
        self.setWindowTitle("Music")
        self.resize(1200, 800)
        self.header = Header(self)
        self.navigation = Navigation(self)
        self.playWidgets = PlayWidgets(self)
        self.detailSings = DetailSings(self)
        self.mainContent = MainContent(self)
        self.nativeMusic = NativeMusic(self)
        self.downloadFrame = DownloadFrame(self)
        self.searchArea = SearchArea(self)
        self.recommendFrame = RecommendFrame(self)
        self.mainContents = QTabWidget()
        self.mainContents.tabBar().setObjectName("mainTab")
        self.systemTray = SystemTray('resource/logo.png', self)
        self.setContents()
        self.addAllPlaylist()
        self.setLines()
        self.setLayouts()
        self.configFeatures()

        with open('QSS/window.qss', 'r') as f:
            self.setStyleSheet(f.read())

    def addAllPlaylist(self):
        self.indexNetEaseSings = NetEaseSingsArea(self.mainContent)
        self.indexXiamiSings = XiamiSingsArea(self.mainContent)
        self.indexQQSings = QQSingsArea(self.mainContent)
        self.mainContent.addTab(self.indexNetEaseSings, "网易云歌单")
        self.mainContent.addTab(self.indexXiamiSings, "虾米歌单")
        self.mainContent.addTab(self.indexQQSings, "QQ音乐歌单")

    def setContents(self):
        self.mainContents.addTab(self.mainContent, '')
        self.mainContents.addTab(self.detailSings, '')
        self.mainContents.addTab(self.nativeMusic, '')
        self.mainContents.addTab(self.downloadFrame, '')
        self.mainContents.addTab(self.recommendFrame, '')
        self.mainContents.addTab(self.searchArea, '')
        self.mainContents.setCurrentIndex(0)

    def setLines(self):
        self.line1 = QFrame(self)
        self.line1.setObjectName("line1")
        self.line1.setFrameShape(QFrame.HLine)
        self.line1.setFrameShadow(QFrame.Plain)
        self.line1.setLineWidth(2)

    def setLayouts(self):
        self.mainLayout = QVBoxLayout()
        self.mainLayout.addWidget(self.header)
        self.mainLayout.addWidget(self.line1)
        self.contentLayout = QHBoxLayout()
        self.contentLayout.setStretch(0, 70)
        self.contentLayout.setStretch(1, 570)
        self.contentLayout.addWidget(self.navigation)
        self.contentLayout.addWidget(self.mainContents)
        self.contentLayout.setSpacing(0)
        self.contentLayout.setContentsMargins(0, 0, 0, 0)
        self.mainLayout.addLayout(self.contentLayout)
        self.mainLayout.addWidget(self.playWidgets)
        self.mainLayout.setStretch(0, 43)
        self.mainLayout.setStretch(1, 0)
        self.mainLayout.setStretch(2, 576)
        self.mainLayout.setStretch(3, 50)
        self.mainLayout.setSpacing(0)
        self.mainLayout.setContentsMargins(0, 0, 0, 0)
        self.setLayout(self.mainLayout)

    def configFeatures(self):
        self.config = ConfigWindow(self)
        self.header.config = ConfigHeader(self.header)
        self.downloadFrame.config = ConfigDownloadFrame(self.downloadFrame)
        self.searchArea.config = ConfigSearchArea(self.searchArea)
        self.navigation.config = ConfigNavigation(self.navigation)
        self.nativeMusic.config = ConfigNative(self.nativeMusic)
        self.mainContent.config = ConfigMainContent(self.mainContent)
        self.detailSings.config = ConfigDetailSings(self.detailSings)
        self.indexNetEaseSings.config = ConfigNetEase(self.indexNetEaseSings)
        self.indexXiamiSings.config = ConfigXiami(self.indexXiamiSings)
        self.indexQQSings.config = ConfigQQ(self.indexQQSings)
        self.systemTray.config = ConfigSystemTray(self.systemTray)
        self.recommendFrame.config = ConfigRecommendFrame(self.recommendFrame)
        self.indexNetEaseSings.config.initThread()
        self.indexXiamiSings.config.initThread()
        self.indexQQSings.config.initThread()
        self.downloadFrame.config.getDownloadSignal()
        self.config.pullRecommendSong()
        screen = QApplication.desktop().availableGeometry()
        self.playWidgets.desktopLyric.resize(screen.width(), 50)
        self.playWidgets.desktopLyric.move(0, screen.height() - 100)

    def closeEvent(self, event):
        self.header.config.saveCookies()
        self.playWidgets.saveCookies()
        self.downloadFrame.config.saveCookies()
        self.nativeMusic.config.saveCookies()
        self.db.commitAndClose()
        self.systemTray.hide()

    def resizeEvent(self, event):
        self.playWidgets.currentMusic.move(0, self.height() - 64 - self.playWidgets.height())


class Header(QFrame):

    def __init__(self, parent=None):
        super(Header, self).__init__()
        self.setObjectName('Header')
        self.parent = parent
        self.loginBox = LoginBox(self)

        with open('QSS/header.qss', 'r', encoding='utf-8') as f:
            self.setStyleSheet(f.read())
        self.setButtons()
        self.setLabels()
        self.setLineEdits()
        self.setLines()
        self.setLayouts()

    def setButtons(self):
        self.closeButton = QPushButton('×', self)
        self.closeButton.setObjectName("closeButton")
        self.closeButton.setMinimumSize(21, 17)
        self.showminButton = QPushButton('_', self)
        self.showminButton.setObjectName("minButton")
        self.showminButton.setMinimumSize(21, 17)
        self.showmaxButton = QPushButton('□')
        self.showmaxButton.setObjectName("maxButton")
        self.showmaxButton.setMaximumSize(16, 16)
        self.loginButton = QPushButton("未登录 ▼", self)
        self.loginButton.setObjectName("loginButton")
        self.prevButton = QPushButton("<")
        self.prevButton.setObjectName("prevButton")
        self.prevButton.setMaximumSize(28, 22)
        self.prevButton.setMinimumSize(28, 22)
        self.nextButton = QPushButton(">")
        self.nextButton.setObjectName("nextButton")
        self.nextButton.setMaximumSize(28, 22)
        self.nextButton.setMinimumSize(28, 22)

    def setLabels(self):
        self.logoLabel = PicLabel(r'resource/format.png', 32, 32)
        self.descriptionLabel = QLabel(self)
        self.descriptionLabel.setText("<b>Music</b>")
        self.userPix = PicLabel(r'resource/no_music.png', 32, 32, r'resource/user_pic_mask.png')
        self.userPix.setMinimumSize(22, 22)
        self.userPix.setObjectName("userPix")

    def setLineEdits(self):
        self.searchLine = SearchLineEdit(self)
        self.searchLine.setPlaceholderText("搜索音乐, 歌手, 歌词, 用户")

    def setLines(self):
        self.line1 = QFrame(self)
        self.line1.setObjectName("line1")
        self.line1.setFrameShape(QFrame.VLine)
        self.line1.setFrameShadow(QFrame.Plain)
        self.line1.setMaximumSize(1, 25)

    def setLayouts(self):

        self.mainLayout = QHBoxLayout()
        self.mainLayout.setSpacing(0)
        self.mainLayout.addWidget(self.logoLabel)
        self.mainLayout.addWidget(self.descriptionLabel)
        self.mainLayout.addSpacing(70)
        self.mainLayout.addWidget(self.prevButton)
        self.mainLayout.addWidget(self.nextButton)
        self.mainLayout.addSpacing(10)
        self.mainLayout.addWidget(self.searchLine)
        self.mainLayout.addStretch(1)
        self.mainLayout.addWidget(self.userPix)
        self.mainLayout.addSpacing(7)
        self.mainLayout.addWidget(self.loginButton)
        self.mainLayout.addSpacing(7)
        self.mainLayout.addWidget(self.line1)
        self.mainLayout.addSpacing(30)
        self.mainLayout.addWidget(self.showminButton)
        self.mainLayout.addWidget(self.showmaxButton)
        self.mainLayout.addSpacing(3)
        self.mainLayout.addWidget(self.closeButton)
        self.setLayout(self.mainLayout)

    def mousePressEvent(self, event):
        if event.buttons() == Qt.LeftButton:
            self.parent.m_drag = True
            self.parent.m_DragPosition = event.globalPos() - self.parent.pos()
            event.accept()

    def mouseMoveEvent(self, event):
        try:
            if event.buttons() and Qt.LeftButton:
                self.parent.move(event.globalPos() - self.parent.m_DragPosition)
                event.accept()
        except AttributeError:
            pass

    def mouseReleaseEvent(self, event):
        if event.buttons() == Qt.LeftButton:
            self.m_drag = False


class Navigation(QScrollArea):
    def __init__(self, parent=None):
        super(Navigation, self).__init__(parent)
        self.parent = parent
        self.frame = QFrame()
        self.setMaximumWidth(200)
        self.setWidget(self.frame)
        self.setWidgetResizable(True)
        self.frame.setMinimumWidth(200)
        self.navigationListFunction = self.none
        self.nativeListFunction = self.none
        self.singsFunction = self.none

        with open('QSS/navigation.qss', 'r') as f:
            style = f.read()
            self.setStyleSheet(style)
            self.frame.setStyleSheet(style)
        self.setLabels()
        self.setListViews()
        self.setLayouts()

    def setLabels(self):
        self.recommendLabel = QLabel(" 推荐")
        self.recommendLabel.setObjectName("recommendLabel")
        self.recommendLabel.setMaximumHeight(27)
        self.myMusic = QLabel(" 我的音乐")
        self.myMusic.setObjectName("myMusic")
        self.myMusic.setMaximumHeight(27)
        self.singsListLabel = QLabel(" 收藏的歌单")
        self.singsListLabel.setObjectName("singsListLabel")
        self.singsListLabel.setMaximumHeight(27)

    def setListViews(self):
        self.navigationList = QListWidget()
        self.navigationList.setMaximumHeight(110)
        self.navigationList.setObjectName("navigationList")
        self.navigationList.addItem(QListWidgetItem(QIcon('resource/music.png'), " 发现音乐"))
        self.navigationList.addItem(QListWidgetItem(QIcon('resource/signal.png'), " 私人FM"))
        self.navigationList.addItem(QListWidgetItem(QIcon('resource/movie.png'), " MV"))
        self.navigationList.setCurrentRow(0)
        self.nativeList = QListWidget()
        self.nativeList.setObjectName("nativeList")
        self.nativeList.setMaximumHeight(100)
        self.nativeList.addItem(QListWidgetItem(QIcon('resource/notes.png'), " 本地音乐"))
        self.nativeList.addItem(QListWidgetItem(QIcon('resource/download_icon.png'), " 我的下载"))
        self.nativeList.addItem(QListWidgetItem(QIcon('resource/recommend_icon.png'), " 专属推荐"))

    def setLayouts(self):
        self.mainLayout = VBoxLayout(self.frame)
        self.mainLayout.addSpacing(10)
        self.mainLayout.addWidget(self.recommendLabel)
        self.mainLayout.addSpacing(3)
        self.mainLayout.addWidget(self.navigationList)
        self.mainLayout.addSpacing(1)
        self.mainLayout.addWidget(self.myMusic)
        self.mainLayout.addSpacing(3)
        self.mainLayout.addWidget(self.nativeList)
        self.mainLayout.addSpacing(1)
        self.mainLayout.addWidget(self.singsListLabel)
        self.mainLayout.addSpacing(1)
        self.mainLayout.addStretch(1)
        self.setContentsMargins(0, 0, 0, 0)

    def setSingsList(self):
        pass

    def none(self):
        pass


class MainContent(ScrollArea):

    def __init__(self, parent=None):
        super(MainContent, self).__init__()
        self.parent = parent
        self.setObjectName("MainContent")

        with open("QSS/mainContent.qss", 'r', encoding='utf-8') as f:
            self.style = f.read()
            self.setStyleSheet(self.style)
        self.tab = QTabWidget()
        self.tab.setObjectName("contentsTab")
        self.mainLayout = QVBoxLayout()
        self.mainLayout.setSpacing(0)
        self.mainLayout.setContentsMargins(0, 0, 0, 0)
        self.mainLayout.addWidget(self.tab)
        self.frame.setLayout(self.mainLayout)

    def addTab(self, widget, name=''):
        self.tab.addTab(widget, name)


class SearchArea(ScrollArea):

    def __init__(self, parent=None):
        super(SearchArea, self).__init__(self)
        self.parent = parent
        self.setObjectName("searchArea")
        with open('QSS/searchArea.qss', 'r', encoding='utf-8') as f:
            self.setStyleSheet(f.read())
        self.mainLayout = QVBoxLayout(self.frame)
        self.titleLabel = QLabel(self.frame)
        self.contentsTab = QTabWidget(self.frame)
        self.mainLayout.addWidget(self.titleLabel)
        self.mainLayout.addWidget(self.contentsTab)
        self.setSingsFrame()

    def setSingsFrame(self):
        self.neteaseSearchFrame = NetEaseSearchResultFrame(self)
        self.contentsTab.addTab(self.neteaseSearchFrame, "网易云")
        self.xiamiSearchFrame = XiamiSearchResultFrame(self)
        self.contentsTab.addTab(self.xiamiSearchFrame, "虾米")
        self.qqSearchFrame = QQSearchResultFrame(self)
        self.contentsTab.addTab(self.qqSearchFrame, 'QQ')

    def setText(self, text):
        self.text = text
        self.titleLabel.setText("搜索<font color='#23518F'>“{0}”</font><br>".format(self.text))


def start():
    app = QApplication(sys.argv)
    eventLoop = QEventLoop(app)
    asyncio.set_event_loop(eventLoop)

    try:
        main = Window()
        main.show()
        main.playWidgets.currentMusic.resize(main.navigation.width(), 64)
        with eventLoop:
            eventLoop.run_forever()
        sys.exit(0)
    except:
        logger.error("got some error", exc_info=True)


if __name__ == '__main__':
    start()
